﻿/*
 * PROJECT:   LVGL PC Simulator using Visual Studio
 * FILE:      LVGL.Simulator.cpp
 * PURPOSE:   Implementation for LVGL ported to Windows Desktop
 *
 * LICENSE:   The MIT License
 *
 * DEVELOPER: Mouri_Naruto (Mouri_Naruto AT Outlook.com)
 */

#include <Windows.h>

#include "resource.h"

#if _MSC_VER >= 1200
 // Disable compilation warnings.
#pragma warning(push)
// nonstandard extension used : bit field types other than int
#pragma warning(disable:4214)
// 'conversion' conversion from 'type1' to 'type2', possible loss of data
#pragma warning(disable:4244)
#endif

#include "lvgl/lvgl.h"
#include "lvgl/examples/lv_examples.h"
#include "lv_demos/lv_demo.h"
#include "lv_drivers/win32drv/win32drv.h"
#include "lv_demos/src/lv_demo_widgets/lv_demo_widgets.h"
#include "lv_demos/src/lv_demo_music/lv_demo_music.h"
#include "lv_demos/src/lv_demo_benchmark/lv_demo_benchmark.h"
#include "lv_demos/src/lv_demo_keypad_encoder/lv_demo_keypad_encoder.h"
#include "lv_demos/src/lv_demo_stress/lv_demo_stress.h"
#include "lvgl/lvgl.h"
#include "lv_port/lv_port_fs.h"
#include "gif_decoder.h"


#if defined(LV_LVGL_H_INCLUDE_SIMPLE)
#include "picture_define.h"
#include "user_knob_config.h"
#else
#include "picture/picture_define.h"
#include "user_app/user_knob_config.h"
#endif

//#define LV_VER_RES_MAX 400
//#define LV_HOR_RES_MAX 400


#if _MSC_VER >= 1200
// Restore compilation warnings.
#pragma warning(pop)
#endif
#include <stdio.h>

uint8_t read_img_data[30 * 1024 * 1024];
uint32_t img_file_len;
void user_read_image_file(void)
{
#define file_path "picture/UI_ALL.bin"
//D:\WorkFiles\git\work-code\lvgl_visual_studio\LVGL.Simulator\img_bin
//#define file_path "D:\\WorkFiles\\git\\work-code\\lvgl_visual_studio\\LVGL.Simulator\\img_bin\\image.bin"
//#define file_path "D:\\WorkFiles\\git\\work-code\\lvgl_visual_studio\\LVGL.Simulator\\img_bin\\222.gif"
    FILE* fp = NULL;
    uint32_t size;
    uint32_t read_len = 0;
    // fp = fopen("D:\\gif_image\\giftest1.gif", "rb"); //
    fp = fopen(file_path, "rb");
    if (fp != NULL)
    {
        //feof(file stream )文件指针到达文件末尾
        //    printf("%x\n", fgetc(fp));
       //  把文件的位置指针移到文件尾
        fseek(fp, 0, SEEK_END);
        // 获取文件长度
        size = ftell(fp);
        printf("file len: %d byte\r\n", size);
        fclose(fp);
    }
    else
        printf("fail to open! \n");
    fp = fopen(file_path, "rb");
    if (fp != NULL)
    {
        read_len = fread(&read_img_data[0], 1, size, fp);
        printf("read len: %d byte\r\n", read_len);
        img_file_len = read_len;
        fclose(fp);
    }
    else
        printf("fail to open! \n");
}
lv_indev_t* indev_encoder;
int32_t encoder_diff;
lv_indev_state_t encoder_state= LV_INDEV_STATE_RELEASED;
static void encoder_read(lv_indev_drv_t* indev_drv, lv_indev_data_t* data)
{
    static uint32_t last_ms = 0;
    data->enc_diff = encoder_diff;
    data->state = encoder_state;
     //printf("tick:%d\n", lv_tick_get()-last_ms);
    encoder_diff = 0;
    last_ms = lv_tick_get();

}



 
void lv_port_indev_init(void)
{
    static lv_indev_drv_t indev_drv;

    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_ENCODER;//设备类型 编码器
    indev_drv.read_cb = encoder_read;//回调函数
    indev_encoder = lv_indev_drv_register(&indev_drv);
}
/**
 * @brief 模拟器上采用键盘的 上下左右按键模拟实物中的编码器  左右和编码器左右功能一样选择，上键表示长按退出 下键表示单击进入
 * @param  
 * @return 
*/
int main(void)
{
    lv_init();
   
    if (!lv_win32_init(
        GetModuleHandleW(NULL),
        SW_SHOW,
        LV_HOR_RES_MAX,
        LV_VER_RES_MAX,
        LoadIconW(GetModuleHandleW(NULL), MAKEINTRESOURCE(IDI_LVGL))))
    {
        return -1;
    }
    /*文件系统支持*/
    lv_port_fs_init();
    lv_win32_add_all_input_devices_to_group(NULL);
    
   // read_gif_file();
   #if 0
    lv_port_indev_init();
    ////指针
    lv_group_t* group;
    ////创建一个group
    group = lv_group_create();
    ////将group加载到输入设备中。注意，下面这个函数的第一个参数是注册输入设备时返回的指向新输入设备的指针
    lv_indev_set_group(indev_encoder, group);
    lv_group_set_default(group);         //设置组(group)为默认模式,后面的部件创建时会自动加入组(group)
    lv_indev_set_group(indev_encoder, group);    //将输入设备
    #endif

    user_read_image_file();

    // lv_demo_benchmark();
  
    //lv_obj_t* tempimg = lv_img_create(lv_scr_act());
    //lv_img_set_src(tempimg, "S:img_bin/A01.bin");
    //lv_obj_align(tempimg, LV_ALIGN_TOP_LEFT, 0, 0);
    //lv_prj_main();

     /*lv_obj_t* logo_ico = lv_img_create(lv_scr_act());
     lv_img_set_src(logo_ico, &humidity);
     lv_obj_set_pos(logo_ico, 24, 32);*/
    knob_gui_init();
    while (!lv_win32_quit_signal)
    {
        lv_task_handler();
        Sleep(1);
        
    }

    return 0;
}
